package model;

import static org.junit.Assert.*;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import javax.xml.bind.JAXBException;

import model.environmentConfig.CommunicationChannel;
import model.environmentConfig.ContactPerson;
import model.environmentConfig.EnvironmentConfiguration;
import model.environmentConfig.Node;
import model.environmentConfig.NodeRectangle;
import model.environmentConfig.NodeState;
import model.environmentConfig.Organization;
import model.environmentConfig.Protocol;
import model.environmentConfig.SecurityZone;
import model.environmentConfig.SoftwarePackage;

import org.junit.Test;

public class XMLInterfaceTest {

	private static final String TEST_XML_NAME_PATH="./Foobar2.xml";
	
	@Test
	public void testXMLSaveAndLoad() {
		Node node = new Node();
		SecurityZone sz=new SecurityZone();
		sz.setName("Foobar zone");
		
		ArrayList<SecurityZone> sza=new ArrayList<SecurityZone>();
		sza.add(sz);
		node.setSecurityZone(sza);
		
		Organization o=new Organization();
		o.setName("foobar inc");
		
		ArrayList<Organization> oa=new ArrayList<Organization>();
		oa.add(o);
		node.setResponsibleOrganization(oa);
		
		SoftwarePackage sp=new SoftwarePackage();
		sp.setName("foobar package");
		sp.setVersion("v39");
		
		ArrayList<SoftwarePackage> spa=new ArrayList<SoftwarePackage>();
		spa.add(sp);
		node.setRequiredSoftwarePackages(spa);
		
		NodeRectangle rec=new NodeRectangle(100, 100, 20, 50);
		node.setDisplayData(rec);
		
		ContactPerson cp=new ContactPerson();
		cp.setName("Mr. foobar");
		cp.setEmail("foobar@foo.bar");
		cp.setPhoneNumber("6549873210");
		
		ArrayList<ContactPerson> cpa=new ArrayList<ContactPerson>();
		cpa.add(cp);
		node.setContactPerson(cpa);
		
		node.setExternalIPAddress("987654321");
		node.setOperatingSystem("FoobarOS");
		node.setNodeState(NodeState.RUNNING_NOTHING);
		node.setComputerName("Foobar's computer");
		node.setInternalIPAddress("1234567879");
		
		HashMap<Long, Node> nodes=new HashMap<Long, Node>();
		Long foo=new Random().nextLong();
		nodes.put(foo, node);
		
		CommunicationChannel channel=new CommunicationChannel();
		channel.setInitiatorPortRange(123, 125);
		channel.setListenerPortRange(321, 324);
		channel.setInitiatorNode(foo);
		channel.setListeningNode(foo);
		
		Protocol p=new Protocol();
		p.setName("foobar protocol");
		channel.setTransportProtocol(p);
		
		Protocol p1=new Protocol();
		p1.setName("barfoo protocol");
		channel.setApplicationProtocol(p1);
		
		
		ArrayList<CommunicationChannel> channels=new ArrayList<CommunicationChannel>();
		channels.add(channel);
		
		EnvironmentConfiguration ec=new EnvironmentConfiguration();
		ec.setChannels(channels);
		ec.setNodes(nodes);
		
		EnvironmentConfiguration envc;
		
		try{
			XMLInterface.saveXMLFile(TEST_XML_NAME_PATH, ec);
			envc=XMLInterface.loadXMLFile(TEST_XML_NAME_PATH);
			
			assertTrue(envc.equals(ec));
		}
		catch(Exception e){
			System.out.println(e.getLocalizedMessage());
			System.out.println(e.getMessage());
			fail("Something went wrong");
		}
	}

}
